home *** CD-ROM | disk | FTP | other *** search
- Path: newbridge.com!gminer
- From: gminer@Newbridge.COM (Glen Miner)
- Newsgroups: comp.lang.c
- Subject: Re: What's better & why
- Date: 21 Feb 1996 12:57:42 GMT
- Organization: Newbridge Networks Corporation
- Message-ID: <4gf4s6$fl0@kannews.ca.newbridge.com>
- References: <31297C5A.E6C@connix.com>
- NNTP-Posting-Host: thor.ca.newbridge.com
-
- >I was just curious what you all though about the following code.
- >Please tell me what is better (faster/Smaller) and why. Or does it make
- >any difference at all. I just though about this while I was programming
- >today?
- >
- >example 1:
- > val = 1;
- > if( what ever...)val = 0;
- >
- >or
- >example 2:
- > if( what ever... )val = 0;
- > else val = 1;
-
- What an interesting question :) I'm going to go home, code this and then
- decompile it into ASM to see if it does it this way (my compiler may be
- smart, though).
-
- If the comiler was dumb, you would get something like the following ASM
- instructions:
-
- example 1:
- mov val, 1
- cmp what, ever
- jne End
- mov val, 0
- End:
-
- 2 memory moves, 1 compare, one conditional jump. Fewer instructions =
- smaller code. 3 or 4 instructions executed.
-
- example 2:
- cmp what, ever
- jne End
- mov val, 0
- jmp Finish
- End: mov val 1
- Finish:
-
- 2 memory moves, 1 compare, one conditional jump, on unconditional jump.
-
- Slightly larger code, one instruction, wee :) Again, 3 or 4 instrucitons
- executed.
-
- But: with the worst case in example 1, you are executing an extra move,
- and with the worst case in example 2, you are executing an extra
- unconditional jump. Which is faster? I don't have my ASM handbook here so
- I couldn't tell you ;) If I had to guess, I'd say the jmp would be faster
- becuase your val data wouldn't be registers and all.
-
- So, if I wanted speed, like I usually do, and I was willing to pay the
- extra few bytes in code size, I'd stay with example 2.
-
- Now, I'm no asm genious, and I don't know how smart my compiler is. It
- may well realize what you are doing and do things an even better way.
-
- I'm still tempted to go home and code it in c, and see what the asm turns
- out to be :)_
-
- Thanx for the interesting question :)
-
- Peace
- --
- Peace
- _______________________________________________________________________________
- Wonko the Sane: gaminer@undergrad.math.uwaterloo.ca -> Peace.Love.Unity.Respect
-